package com.huike.game.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.base.Game60;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

public class Game60CollectActivity extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private LinearLayout ll_game60_collect;
    private ListView lv_game60_collect;
    private ArrayList<Game60.DataBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game60_collect);
        list = new ArrayList<>();
        initView();
        initData();

        lv_game60_collect.setAdapter(new ViewList());

    }

    private void initData() {

        SharedPreferences sp = getSharedPreferences("collectList", MODE_PRIVATE);
        String data = sp.getString("data", null);

        if (data != null) {
            String[] split = data.split("-");

            for (int i = 0; i < split.length; i++) {
                Game60.DataBean json = new Gson().fromJson(split[i], Game60.DataBean.class);
                list.add(json);
            }
        }else{
            ll_game60_collect.setVisibility(View.VISIBLE);
            lv_game60_collect.setVisibility(View.GONE);
        }

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);
        tv_content_title.setText("用户收藏");
        tv_left_text.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);


        ll_game60_collect = (LinearLayout) findViewById(R.id.ll_game60_collect);
        lv_game60_collect = (ListView) findViewById(R.id.lv_game60_collect);
    }

    class ViewList extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(Game60CollectActivity.this, R.layout.game_collect60, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            Game60.DataBean bean = list.get(i);
            holder.tv_game60_name.setText("用户名称：" +bean.getName());
            holder.tv_game60_username.setText("姓名：" +bean.getUser());
            holder.tv_game60_phone.setText("电话："+bean.getPhone());


            return view;
        }


    }


    class ViewHolder {
        public View rootView;
        public ImageView iv_game60_head;
        public TextView tv_game60_username;
        public TextView tv_game60_name;
        public TextView tv_game60_phone;
        public TextView tv_game60_super;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_game60_head = (ImageView) rootView.findViewById(R.id.iv_game60_head);
            this.tv_game60_username = (TextView) rootView.findViewById(R.id.tv_game60_username);
            this.tv_game60_name = (TextView) rootView.findViewById(R.id.tv_game60_name);
            this.tv_game60_phone = (TextView) rootView.findViewById(R.id.tv_game60_phone);
            this.tv_game60_super = (TextView) rootView.findViewById(R.id.tv_game60_super);
        }

    }

}
